
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Model _meta API &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Related objects reference" href="relations.html" />
    <link rel="prev" title="Constraints reference" href="constraints.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="constraints.html" title="Constraints reference">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="relations.html" title="Related objects reference">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-meta">
            
  <div class="section" id="s-module-django.db.models.options">
<span id="s-model-meta-api"></span><span id="module-django.db.models.options"></span><span id="model-meta-api"></span><h1>Model <code class="docutils literal notranslate"><span class="pre">_meta</span></code> API<a class="headerlink" href="#module-django.db.models.options" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="django.db.models.options.Options">
<em class="property">class </em><code class="descname">Options</code><a class="reference internal" href="../../_modules/django/db/models/options.html#Options"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.options.Options" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The model <code class="docutils literal notranslate"><span class="pre">_meta</span></code> API is at the core of the Django ORM. It enables other
parts of the system such as lookups, queries, forms, and the admin to
understand the capabilities of each model. The API is accessible through
the <code class="docutils literal notranslate"><span class="pre">_meta</span></code> attribute of each model class, which is an instance of an
<code class="docutils literal notranslate"><span class="pre">django.db.models.options.Options</span></code> object.</p>
<p>Methods that it provides can be used to:</p>
<ul class="simple">
<li>Retrieve all field instances of a model</li>
<li>Retrieve a single field instance of a model by name</li>
</ul>
<div class="section" id="s-field-access-api">
<span id="s-model-meta-field-api"></span><span id="field-access-api"></span><span id="model-meta-field-api"></span><h2>Field access API<a class="headerlink" href="#field-access-api" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-retrieving-a-single-field-instance-of-a-model-by-name">
<span id="retrieving-a-single-field-instance-of-a-model-by-name"></span><h3>Retrieving a single field instance of a model by name<a class="headerlink" href="#retrieving-a-single-field-instance-of-a-model-by-name" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.models.options.Options.get_field">
<code class="descclassname">Options.</code><code class="descname">get_field</code>(<em>field_name</em>)<a class="reference internal" href="../../_modules/django/db/models/options.html#Options.get_field"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.options.Options.get_field" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the field instance given a name of a field.</p>
<p><code class="docutils literal notranslate"><span class="pre">field_name</span></code> can be the name of a field on the model, a field
on an abstract or inherited model, or a field defined on another
model that points to the model. In the latter case, the <code class="docutils literal notranslate"><span class="pre">field_name</span></code>
will be (in order of preference) the <a class="reference internal" href="fields.html#django.db.models.ForeignKey.related_query_name" title="django.db.models.ForeignKey.related_query_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">related_query_name</span></code></a>
set by the user, the <a class="reference internal" href="fields.html#django.db.models.ForeignKey.related_name" title="django.db.models.ForeignKey.related_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">related_name</span></code></a> set by the user, or
the name automatically generated by Django.</p>
<p><a class="reference internal" href="fields.html#django.db.models.Field.hidden" title="django.db.models.Field.hidden"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Hidden</span> <span class="pre">fields</span></code></a> cannot be retrieved
by name.</p>
<p>If a field with the given name is not found a
<a class="reference internal" href="../exceptions.html#django.core.exceptions.FieldDoesNotExist" title="django.core.exceptions.FieldDoesNotExist"><code class="xref py py-class docutils literal notranslate"><span class="pre">FieldDoesNotExist</span></code></a> exception will be
raised.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>

<span class="go"># A field on the model</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">)</span>
<span class="go">&lt;django.db.models.fields.CharField: username&gt;</span>

<span class="go"># A field from another model that has a relation with the current model</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="s1">&#39;logentry&#39;</span><span class="p">)</span>
<span class="go">&lt;ManyToOneRel: admin.logentry&gt;</span>

<span class="go"># A non existent field</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="s1">&#39;does_not_exist&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">FieldDoesNotExist</span>: <span class="n">User has no field named &#39;does_not_exist&#39;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-retrieving-all-field-instances-of-a-model">
<span id="retrieving-all-field-instances-of-a-model"></span><h3>Retrieving all field instances of a model<a class="headerlink" href="#retrieving-all-field-instances-of-a-model" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.models.options.Options.get_fields">
<code class="descclassname">Options.</code><code class="descname">get_fields</code>(<em>include_parents=True</em>, <em>include_hidden=False</em>)<a class="reference internal" href="../../_modules/django/db/models/options.html#Options.get_fields"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.options.Options.get_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a tuple of fields associated with a model. <code class="docutils literal notranslate"><span class="pre">get_fields()</span></code> accepts
two parameters that can be used to control which fields are returned:</p>
<dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">include_parents</span></code></dt>
<dd><code class="docutils literal notranslate"><span class="pre">True</span></code> by default. Recursively includes fields defined on parent
classes. If set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, <code class="docutils literal notranslate"><span class="pre">get_fields()</span></code> will only search for
fields declared directly on the current model. Fields from models that
directly inherit from abstract models or proxy classes are considered
to be local, not on the parent.</dd>
<dt><code class="docutils literal notranslate"><span class="pre">include_hidden</span></code></dt>
<dd><code class="docutils literal notranslate"><span class="pre">False</span></code> by default. If set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">get_fields()</span></code> will include
fields that are used to back other field’s functionality. This will
also include any fields that have a <code class="docutils literal notranslate"><span class="pre">related_name</span></code> (such
as <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a>, or
<a class="reference internal" href="fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">ForeignKey</span></code></a>) that start with a “+”.</dd>
</dl>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_fields</span><span class="p">()</span>
<span class="go">(&lt;ManyToOneRel: admin.logentry&gt;,</span>
<span class="go"> &lt;django.db.models.fields.AutoField: id&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: password&gt;,</span>
<span class="go"> &lt;django.db.models.fields.DateTimeField: last_login&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_superuser&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: username&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: first_name&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: last_name&gt;,</span>
<span class="go"> &lt;django.db.models.fields.EmailField: email&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_staff&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_active&gt;,</span>
<span class="go"> &lt;django.db.models.fields.DateTimeField: date_joined&gt;,</span>
<span class="go"> &lt;django.db.models.fields.related.ManyToManyField: groups&gt;,</span>
<span class="go"> &lt;django.db.models.fields.related.ManyToManyField: user_permissions&gt;)</span>

<span class="go"># Also include hidden fields.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_fields</span><span class="p">(</span><span class="n">include_hidden</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">(&lt;ManyToOneRel: auth.user_groups&gt;,</span>
<span class="go"> &lt;ManyToOneRel: auth.user_user_permissions&gt;,</span>
<span class="go"> &lt;ManyToOneRel: admin.logentry&gt;,</span>
<span class="go"> &lt;django.db.models.fields.AutoField: id&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: password&gt;,</span>
<span class="go"> &lt;django.db.models.fields.DateTimeField: last_login&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_superuser&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: username&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: first_name&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: last_name&gt;,</span>
<span class="go"> &lt;django.db.models.fields.EmailField: email&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_staff&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_active&gt;,</span>
<span class="go"> &lt;django.db.models.fields.DateTimeField: date_joined&gt;,</span>
<span class="go"> &lt;django.db.models.fields.related.ManyToManyField: groups&gt;,</span>
<span class="go"> &lt;django.db.models.fields.related.ManyToManyField: user_permissions&gt;)</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Model <code class="docutils literal notranslate"><span class="pre">_meta</span></code> API</a><ul>
<li><a class="reference internal" href="#field-access-api">Field access API</a><ul>
<li><a class="reference internal" href="#retrieving-a-single-field-instance-of-a-model-by-name">Retrieving a single field instance of a model by name</a></li>
<li><a class="reference internal" href="#retrieving-all-field-instances-of-a-model">Retrieving all field instances of a model</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="constraints.html"
                        title="previous chapter">Constraints reference</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="relations.html"
                        title="next chapter">Related objects reference</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/models/meta.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="constraints.html" title="Constraints reference">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="relations.html" title="Related objects reference">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>